//
//  DDBossMeViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/20.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossMeViewController.h"
#import "DDBossPublishedJobViewController.h"
#import "UINavigationBar+Awesome.h"
#import "DDSettingViewController.h"
#import "DDActivenessViewController.h"
#import "DDAccountBalanceViewController.h"
#import "DDBossHeaderView.h"
#import "DDBossInfoService.h"
#import "DDBossInfo.h"
#import "DDMeCell.h"
#import "AppDelegate.h"
#import "DDBossEditInfoViewController.h"
#import "DDCompanyHomeViewController.h"
#import "DDBossInterestingCattleViewController.h"
#import "DDBossCommunicatedCattleViewController.h"
#import "DDCompanyMicroPageViewController.h"
#import "DDBossActivenessViewController.h"

#define NAVBAR_CHANGE_POINT 50

@interface DDBossMeViewController ()

@property (strong, nonatomic) DDBossInfoService *infoService;
@property (strong, nonatomic) DDBossInfo *bossInfo;

@end

@implementation DDBossMeViewController

#pragma mark
#pragma mark - View LifeCycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.infoService = [[DDBossInfoService alloc] init];
    self.bossInfo = [[DDBossInfo alloc] init];
    
    [self setupData];
    [self setupView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupData) name:NOTI_BOSS_EDIT_PERSIONINFO object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark
#pragma mark - Private Methods

- (void)setupData
{
    WEAKSELF
    [self.infoService queryBossInfoWithSuccessBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            DDBossInfo *model = [DDBossInfo yy_modelWithJSON:responseObject[@"data"]];
            NSLog(@"model____%@",model);
            weakSelf.bossInfo = model;
            [weakSelf updateView];
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

- (void)setupView
{
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    
    [self addLeftButtonItemWithImage:[UIImage imageNamed:@"me_icon_share"] action:@selector(meShareClick)];
    [self addRightButtonItemWithImage:[UIImage imageNamed:@"me_icon_setting"] action:@selector(meSettingClick)];
    
    WEAKSELF
    DDBossHeaderView *headerView = [DDBossHeaderView bossHeaderView];
    [headerView.publishJobButton addTarget:self action:@selector(publishJobClick:) forControlEvents:UIControlEventTouchUpInside];
    [headerView.editInfoButton addTarget:self action:@selector(editInfoClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [headerView.attentionView bk_whenTapped:^{
        DDBossActivenessViewController *active = [[DDBossActivenessViewController alloc] initWithNibName:@"DDActivenessViewController" bundle:nil];
        active.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:active animated:YES];
    }];

    [headerView.activenessView bk_whenTapped:^{
        DDBossActivenessViewController *active = [[DDBossActivenessViewController alloc] initWithNibName:@"DDActivenessViewController" bundle:nil];
        active.hidesBottomBarWhenPushed = YES;
        [weakSelf.navigationController pushViewController:active animated:YES];
    }];
    
    self.tableView.tableHeaderView = headerView;
}

- (void)updateView
{
    DDBossHeaderView *headerView = (DDBossHeaderView *)self.tableView.tableHeaderView;
    [headerView.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[DDAPIGenerate imageURLWithPath:self.bossInfo.logo]] placeholderImage:[UIImage imageNamed:@"common_avatar"]];
    
    NSMutableArray *tempArray = [@[] mutableCopy];
    
    if ([self.bossInfo.name length] > 1) {

        [tempArray addObject:self.bossInfo.name];
    }
    if ([self.bossInfo.companyshortname length] > 1) {

        [tempArray addObject:self.bossInfo.companyshortname];
    }
    
    if ([self.bossInfo.positionname length] > 1) {

        [tempArray addObject:self.bossInfo.positionname];
    }
    
    headerView.infoLabel.text = [tempArray componentsJoinedByString:@"|"];
    
    headerView.activenessLabel.text = self.bossInfo.activeness;
    headerView.attentionLabel.text = [NSString stringWithFormat:@"%@%%",self.bossInfo.attention];
    
    [self.tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.navigationItem.title = @"我是BOSS";
    self.tableView.delegate = self;
    [self scrollViewDidScroll:self.tableView];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tableView.delegate = nil;
    [self.navigationController.navigationBar lt_reset];
}


- (NSMutableArray *)getBossMeTitleArray
{
    NSMutableArray *titles = [[NSMutableArray alloc] init];
    [titles addObject:@[
//                       @{@"title": @"账户余额",@"image":@"boss_me_icon_money"},
                         @{@"title": @"公司主页",@"image":@"me_icon_interesting"},
//                         @{@"title": @"个性视频",@"image":@"boss_me_icon_video"},
                         @{@"title": @"分享我的微主页",@"image":@"boss_me_icon_share"}]];
    
    [titles addObject:@[@{@"title": @"感兴趣的牛人",@"image":@"me_icon_interesting"},
                        @{@"title": @"沟通过的牛人",@"image":@"me_icon_chat"}]];
    
    [titles addObject:@[@{@"title": @"当前为BOSS版",@"image":@"boss_me_icon_switch"}]];
    
    return titles;
}

- (void)meShareClick
{
    DDCompanyMicroPageViewController *vc = [[DDCompanyMicroPageViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)meSettingClick
{
    mySettingVC *setting = [[mySettingVC alloc] init];//swift
    setting.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setting animated:YES];
}


#pragma mark
#pragma mark - Event Response

- (void)publishJobClick:(UIButton *)button
{
    DDBossPublishedJobViewController *publish = [[DDBossPublishedJobViewController alloc] init];
    publish.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:publish animated:YES];
}

- (void)editInfoClick:(UIButton *)button
{
    DDBossEditInfoViewController *vc = [[DDBossEditInfoViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark
#pragma mark - CustomDelegate

#pragma mark
#pragma mark - UIScrollView Delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor * color = [UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > NAVBAR_CHANGE_POINT) {
        CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}

#pragma mark - UITableView DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self getBossMeTitleArray][section] count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self getBossMeTitleArray].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"DDMeCell";
    
    DDMeCell *cell = (DDMeCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDMeCell" owner:self options:nil] lastObject];
    }
    
    NSArray *array = [self getBossMeTitleArray][indexPath.section];
    NSDictionary *dic = array[indexPath.row];
    cell.titleLabel.text = dic[@"title"];
    cell.iconImage.image = [UIImage imageNamed:dic[@"image"]];
    
//    if (indexPath.row == 0 && indexPath.section == 0) {
//        // 账户余额
//        cell.subTitleLabel.text = [NSString stringWithFormat:@"￥%@", self.bossInfo.moneybalance];
//        cell.subTitleLabel.textColor = COLOR_FONT_RED;
//    } else
        if (indexPath.row == 0 && indexPath.section == 1) {
        // 感兴趣的牛人
        cell.subTitleLabel.textColor = COLOR_FONT_DARK_GRAY;
        cell.subTitleLabel.text = self.bossInfo.interestedjobfinderscount;
    } else if (indexPath.row == 0 && indexPath.section == 2) {
        cell.subTitleLabel.textColor = COLOR_FONT_DARK_GRAY;
        if ([DDClientInfoManager clientType] == DDClientTypeCattle) {
            cell.titleLabel.text = @"当前为牛人端";
            cell.subTitleLabel.text = @"切换至Boss端";
        } else {
            cell.titleLabel.text = @"当前为Boss端";
            cell.subTitleLabel.text = @"切换至牛人端";
        }
    } else {
        cell.subTitleLabel.text = @"";
    }
    
    return cell;
}

#pragma mark -
#pragma mark  UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = nil;
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
//                case 0: {
//                 // 账户余额
//                    vc = [[DDAccountBalanceViewController alloc] init];
//                }
//                    break;
                case 0: {
                    // 公司主页
                    vc = [[DDCompanyHomeViewController alloc] init];
                }
                    break;
//                case 2: {
//                    // 个性视频先不做
//                }
//                    break;
                case 1: {
                    
                    /// 我的微主页
                    DDCompanyMicroPageViewController *vc = [[DDCompanyMicroPageViewController alloc] init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
                    break;
                    
                default:
                    break;
            }

        }
            break;
            
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    // 感兴趣的牛人
                    DDBossInterestingCattleViewController *interesting = [[DDBossInterestingCattleViewController alloc] init];
                    vc = interesting;
                }
                    break;
                case 1:
                {
                    // 沟通过的牛人
                    DDBossCommunicatedCattleViewController *comminicate = [[DDBossCommunicatedCattleViewController alloc] init];
                    vc = comminicate;
                }
                    break;
                    
                default:
                    break;
            }
        }
         
            break;
            
        case 2:
        {
            [[LDShowHUB shareInstance] showStaus:@"切换中，请稍候..."];
            DDMeViewController *vc = (DDMeViewController *)self;
            [DDClientInfoManager changeClientType:vc];
        }
            
            break;
        default:
            break;
    }
    
    if (vc) {
        vc.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}



#pragma mark
#pragma mark - Getters and Setters

@end
